function Rights(){
	this.table=this.InitTable();
}

Rights.prototype.InitTable = function() {
	var table;
	var _this=this;
	layui.use('table', function() {
	    table = layui.table;
		table.render({
			elem : '#list'
			,height : $(window).height() - 100
			,url : 'get_list' // 数据接口
			,page : true // 开启分页
			,limit : 30
			,id: 'rolelist'
			,method:"post"
			,cols : [ [ // 表头
			           {type:'numbers'}
			           ,{field : 'rolename',title : '角色名称',width : 170,align : 'center'}
			           , {field : 'roletype',title : '类型',width : 150,align : 'center'}
			           //, {field : 'createtime',title : '时间',width : 170,align : 'center'}
			           , {field : 'remark',title : '描述',width : 200,align : 'center'}
			           , {field : 'usercount',title : '人员权限',width : 100,align : 'center'}
			           , {field : 'menucount',title : '资源权限',width : 100,align : 'center'}
			           , {title : '操作',width : 120,align : 'left',toolbar : '#operate'}
		            ] ]
		    ,request:{
				type:'post'
			}
		    ,even: true //开启隔行背景
			,where:{rolename: $("#rolename").val()}
		    ,done: function(res, page, count){
		    	$("[data-field='roletype']").children().each(function(){  
                    if($(this).text()=='0'){  
                       $(this).text("普通民警")  
                    }
                    else if($(this).text()=='1'){  
                        $(this).text("部门管理员")  
                     }
                    else if($(this).text()=='2'){  
                       $(this).text("系统管理员")  
                    }
               })  
		    }
		    
		});

		
		 //监听工具条
	    table.on('tool(list)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	        layer.msg('ID：'+ data.id + ' 的查看操作');
	      } else if(obj.event === 'del'){
	    	  
	        top.mylayui.confirm('确定要删除吗？',{icon: 0}, function(index,dd){
	        	 top.mylayui.close(index);
	        	 vRole.deleteentity(data.roleid);
	        });
	      } else if(obj.event === 'edit'){
	        //layer.alert('编辑行：<br>'+ JSON.stringify(data))
	    	  _this.OpTab(2,data);
	      }
	      else if(obj.event === 'btnuser'){
	    	  vRights.openadd(data.roleid,1);
		   }
	      else if(obj.event === 'btnmenu'){
	    	  vRights.openadd(data.roleid,2);
		   }
	      else if(obj.event === 'btnorg'){
	    	  vRights.openadd(data.roleid,3);
		   }
	      
	    });



	});
	
	return table;
}

Rights.prototype.searchtable=function(){
	this.table.reload('rolelist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    ,where: {  
	  	  rolename: $("#rolename").val()  
	    }  
	}); 
}

Rights.prototype.openadd=function(roleid,flag){
	
	var url='';
	var title='';
	var area=[];
	switch(flag){
	case 1:
		url='rights/rights_user';
		title='设置用户';
		area=['700px', '450px'];
		break;
	case 2:
		url='rights/rights_menu';
		title='设置资源';
		area=['700px', '400px'];
		break;
	case 3:
		url='rights/rights_org';
		title='数据权限';
		area=['700px', '450px'];
		break;
	}
	top.mylayui.open({
         type: 2,
         title: title,
         shadeClose: true,
         shade: 0.5,
         area: area,
         content: url+'?roleid='+roleid
       }); 
	
	var iframeele=top.$(".layui-layer-content").find('iframe');
	iframeele.css({"height":$(iframeele).height()-10});

}

Rights.prototype.addresult=function(result,msg){
	if(result>0){
		top.mylayui.msg('操作成功', {icon: 1});
		top.mylayui.closeAll('iframe');
		vUser.searchtable();
	}
	else{
		if(msg){
			top.mylayui.msg(msg, {icon: 0});
		}else{
			top.mylayui.msg('操作失败', {icon: 0});
		}
	}
}

Rights.prototype.addroleuser=function(roleid,userids){
	$.ajax({
		type:"POST",
		url:"insert_roleuser",
		data:{roleid:roleid,userids:userids},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				top.mylayui.closeAll('iframe');
				document.location.reload();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}


Rights.prototype.addrolemenu=function(roleid,menuids){
	$.ajax({
		type:"POST",
		url:"insert_rolemenu",
		data:{roleid:roleid,menuids:menuids},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				top.mylayui.closeAll('iframe');
				document.location.reload();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}

Rights.prototype.addroleorg=function(roleid,orgids){
	$.ajax({
		type:"POST",
		url:"insert_roleorg",
		data:{roleid:roleid,orgids:orgids},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				top.mylayui.closeAll('iframe');
				document.location.reload();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}


var vRights=null;
$(function(){
	vRights=new Rights();
})